<template>
  <div class="formplane">
    <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
      <el-form-item label="角色名称">
        <el-input v-model="formData.frolename" placeholder="请输入角色名称" :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input type="textarea" v-model="formData.fremark" placeholder="请输入角色描述" :rows="2" :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item size="large" class="buttonitem">
        <el-button type="primary" @click="submitForm" v-if="isreadonly !== true">提交</el-button>
        <el-button @click="close">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { selectOne, SaveDataInfo } from '@/api/tappRole'
export default {
  components: {},
  props: ['datainfo', 'isreadonly'],
  data () {
    return {
      formData: {
        frolename: undefined,
        fremark: undefined,
        fid: undefined
      }
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () {
    if (this.datainfo !== undefined && this.datainfo !== null) {
      if (this.datainfo.fid !== undefined && this.datainfo.fid !== null) {
        selectOne(this.datainfo.fid).then(res => {
          if (res.code === 0) {
            this.formData.fid = this.datainfo.fid
            this.formData.frolename = res.data.frolename
            this.formData.fremark = this.datainfo.fremark
          }
        })
      } else {
        Object.assign(this.formData, this.datainfo)
      }
    }
  },
  methods: {
    submitForm () {
      if (
        this.formData.frolename === '' ||
        this.formData.frolename === undefined
      ) {
        this.$message.success('请输入角色名称')
        return
      }
      SaveDataInfo(this.formData).then(res => {
        if (res.code === 0) {
          this.formData.fid = res.data.fid
          this.$emit('savedata', this.formData)
          this.$message.success('保存成功！')
        }
      })
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>
<style scoped>
.formplane {
  margin: 15px;
}
.buttonitem >>> .el-form-item__content {
  float: right;
  height: 100%;
}
</style>
